import numpy as np
import pandas as pd
from scipy.stats import pearsonr


def PeasonCorMatrix(variates):
    all_columns = list(variates.columns)
    corr_rows = []
    for idx1 in range(len(all_columns)):
        col1 = np.array(variates[all_columns[idx1]])
        row = []
        for idx2 in range(len(all_columns)):
            if idx2 < idx1:
                row.append(corr_rows[idx2][idx1])
            elif idx1 == idx2:
                row.append(None)
            else:
                col2 = np.array(variates[all_columns[idx2]])
                corr, _ = pearsonr(col1, col2)
                row.append(abs(corr))
        corr_rows.append(row)

    corr_df = pd.DataFrame(corr_rows, columns=all_columns)
    return corr_df


if __name__ == '__main__':
    fp1 = r'label_file\calculate_result\result_gt_0326.csv'
    fp2 = r'D:\wechat_file\WeChat Files\wxid_9944169443012\FileStorage\File\2021-03\chest_score(1).xls'
    cols = ['布局评分', '摆位评分', '灰阶评分', '锐度评分', '整体印象']

    data1 = pd.read_csv(fp1)
    data2 = pd.read_excel(fp2, sheet_name=None)

    result = data1.copy()

    names = []
    for name in data2:
        if name == 'exhjm':
            continue
        names.append(name)
        df = data2[name][['studyInstUid', ]+cols].rename(columns={col: col + '_' + name for col in cols})
        result = pd.merge(result, df, how='left', on='studyInstUid')
        print()

    corr_rows = []
    for col in cols:
        row = [col, ]
        item_cols = [col + '_' + name for name in names]
        PeasonCorMatrix(result[result[item_cols[0]].notna()][item_cols]).to_csv(r'label_file\calculate_result\{}.csv'.format(col), index=False)
        average = result[item_cols].apply(lambda row: np.mean(row), axis=1)
        for para in list(data1.columns)[1:]:
            tmp_df = pd.concat([average, result[para]], axis=1)
            tmp_df = tmp_df[(tmp_df[0].notna()) & (tmp_df[para].notna())]
            try:
                row.append(abs(pearsonr(np.array(tmp_df[0]), np.array(tmp_df[para]))[0]))
            except:
                print()
        corr_rows.append(row)

    pd.DataFrame(corr_rows, columns=['name'] + list(data1.columns)[1:]).to_csv(r'label_file/calculate_result/average_corr.csv', index=False)
